var root = document.getElementById("root");
var arrc = new Array("A","B","C","D");

var qid = localStorage.getItem("m101qid");
var title = localStorage.getItem("m101title");

function init()
{	
	if(qid==null||qid=='')
		mui.back();
	document.getElementById("a1011h1").innerHTML=title;
	
	mui.ajax(ASYURI+'A1011.do',{
		data:{
			'qid':qid
		},
		dataType:'JSON',
		type:'POST',
		timeout:10000,
		success:function(data){
			var edata = eval('('+data+')');
			var len = edata.length;
			for(var i=0;i<len;i++)
			{
				var obj = edata[i];
				var sobj = obj[i+1];
				
				var body = sobj.body;
				var card = cardInit(sobj.type);				//构建主要卡片
				var head = cardHeadInit((i+1)+"."+body);	//构建卡片的页眉，即问卷主题目
				card.appendChild(head);						//将页眉添加到卡片中
				var foot = cardFootInit();					//添加页脚，即具体选项
				
				if(sobj.type=='A')
				{
					//确定选项个数和选项名称
					var slen = Object.keys(sobj).length;
					var name =genName(i);
					
					//生成属性单选按钮
					var mfoot = divInit();
					foot.appendChild(mfoot);
					mfoot.appendChild(radioInit("A",sobj.A,name));
					mfoot.appendChild(radioInit("B",sobj.B,name));
					foot.appendChild(mfoot);
					
					if(slen > 4){
						var nfoot = divInit();
						nfoot.appendChild(radioInit("C",sobj.C,name));
						if(sobj.D!=null&&sobj!='')
							nfoot.appendChild(radioInit("D",sobj.D,name));
						foot.appendChild(nfoot);
					}
				}else if(sobj.type=='B'){
					var slen = Object.keys(sobj).length;
					var name = genName(i);

					var mfoot = divInit();
					foot.appendChild(mfoot);
					mfoot.appendChild(checkboxInit("A",sobj.A,name));
					mfoot.appendChild(checkboxInit("B",sobj.B,name));
					foot.appendChild(mfoot);
					
					if(slen > 4){
						var nfoot = divInit();
						nfoot.appendChild(checkboxInit("C",sobj.C,name));
						if(sobj.D!=null&&sobj!='')
							nfoot.appendChild(checkboxInit("D",sobj.D,name));
						foot.appendChild(nfoot);
					}
				}else{
					var area = document.createElement("textarea");
					area.setAttribute("id",genName(i));
					foot.appendChild(area);
				}
				
				card.appendChild(foot);				//将页脚添加到卡片中
				root.appendChild(card);				//将卡片添加到主节点中
			}
		},
		error:function(xhr,type,errorThrown){
			mui.toast("error");
			console.log(xhr+":"+type+"-"+errorThrown);
		}
	});
}

function cardInit(type)
{
	var card = document.createElement("div");
	card.setAttribute("class","mui-card");
	card.setAttribute("id","card");
	card.setAttribute("type",type)
	return card;
}
function cardHeadInit(headInfo)
{
	var head = document.createElement("div");
	head.setAttribute("class","mui-card-header");
	head.innerHTML=headInfo;
	return head;
}
function cardFootInit()
{
	var foot = document.createElement("div");
	foot.setAttribute("class","mui-card-footer");
	return foot;
}

function radioInit(choice,body,name)
{
	var radio = document.createElement("div");
	radio.setAttribute("class","mui-input-row mui-radio");
	var label = document.createElement("label");
	label.innerHTML=choice+'.'+body;
	radio.appendChild(label);
	var input = document.createElement("input");
	input.setAttribute("name",name);
	input.setAttribute("type","radio");
	input.setAttribute("value",choice);
	radio.appendChild(input);
	return radio;
}
function checkboxInit(choice,body,name)
{
	var checkbox = document.createElement("div");
	checkbox.setAttribute("class","mui-input-row mui-checkbox");
	var label = document.createElement("label");
	label.innerHTML = choice+'.'+body;
	checkbox.appendChild(label);
	var input = document.createElement("input");
	input.setAttribute("name",name);
	input.setAttribute("type","checkbox");
	input.setAttribute("value",choice);
	checkbox.appendChild(input);
	return checkbox;
}

function divInit()
{
	return document.createElement("div");
}

function genName(i)
{
	var c = i+1;
	return "C"+c;
}

/**
 * 问卷回答的上传方法
 */
mui("body").on("tap","#a1",function(){
	var uid = localStorage.getItem("uid");
	if(uid==null||uid=='')
	{
		mui.toast("您必须先登录才能进行此操作");
		/**
		 * 可以考虑加个跳转登陆页面的确认表
		 */
		return;
	}
	
	var jobj = {};
	
	var card = document.getElementsByClassName("mui-card");
	var clen = card.length;
	for(var i=0;i<clen;i++)
	{
		var tag 	= i+1;
		var type 	= card[i].getAttribute("type");
		var name	= "C" + tag;
		var answer 	= getVals(type,name);
		if(answer==null||answer.length==0)
		{
			mui.toast("您还有第"+tag+"题未输入答案哦");
			return;
		}
		
		jobj[tag]={'type':type,'body':answer};
		console.log(jobj);
	}
	
	var udata = {
	'uid' : uid,
	'qid' : qid,
	'data' : jobj
	}
	
	console.log(udata);

	mui.ajax(ASYURI+'A1013.do',{
		data:udata,
		dataType:'json',
		type:'post',
		timeout:10000,
		success : function(data){
			var edata = data;
			if(edata.tag=='0')
			mui.toast("您已经成功回答了此问卷！");
			else if(edata.tag=='1')
			mui.toast("您已经回答过了此问卷，请不要重复作答哦！");
			else if(edata.tag=='2')
			mui.toast("网络连接故障，请稍后再试！");
			else
			mui.toast("不可能发生的情况，不要在试了。");
		},
		error : function(xhr,type,errorThrown){
			console.log(xhr+":"+type+"-"+errorThrown);
		}
	});
});

function getVals(type,name){
	if(type=='A')		return getRadio(name); 
	else if(type=='B')	return getCheck(name);
	else				return getTexta(name);
}
function getRadio(name)
{
	var rdsObj = document.getElementsByName(name);
	for(i=0;i<rdsObj.length;i++)
	{
		if(rdsObj[i].checked){return rdsObj[i].value;}
	}
	return null;
}
function getCheck(name)
{
	var rdsObj 		= document.getElementsByName(name);
	var checkVal 	= "";
	for(i=0;i<rdsObj.length;i++)
	{
		if(rdsObj[i].checked){checkVal += rdsObj[i].value;}
	}
	return checkVal;
}
function getTexta(id)
{
	console.log(id);
	var area = document.getElementById(id);
	return area.value;
}


init();